Skip to main content

Overview

The zero_shot_metadata module provides pre-defined text templates and class names for zero-shot image classification. These templates are used with build_zero_shot_classifier to create robust classifiers without training. Source: src/open_clip/zero_shot_metadata.py

Template Collections

OPENAI_IMAGENET_TEMPLATES

A comprehensive collection of 80 text templates derived from OpenAI’s CLIP research. These templates provide diverse contextual variations to improve classification robustness. Examples of templates:
  • lambda c: f'a photo of a {c}.'
  • lambda c: f'a bad photo of a {c}.'
  • lambda c: f'a photo of many {c}.'
  • lambda c: f'a sculpture of a {c}.'
  • lambda c: f'a low resolution photo of the {c}.'
  • lambda c: f'a rendering of a {c}.'
  • lambda c: f'graffiti of a {c}.'
  • lambda c: f'a cropped photo of the {c}.'
  • lambda c: f'a bright photo of a {c}.'
  • lambda c: f'a dark photo of the {c}.'
  • lambda c: f'a black and white photo of the {c}.'
  • lambda c: f'a painting of the {c}.'
  • lambda c: f'a {c} in a video game.'
  • lambda c: f'itap of a {c}.' (“I took a picture of”)
Total: 80 templates covering various visual styles, conditions, and contexts. Source: src/open_clip/zero_shot_metadata.py:2

SIMPLE_IMAGENET_TEMPLATES

A smaller, curated subset of 7 templates from the OpenAI CLIP Prompt Engineering notebook. This provides a good balance between accuracy and computational efficiency. Templates:
Source: src/open_clip/zero_shot_metadata.py:88 Reference: OpenAI CLIP Prompt Engineering Notebook

Class Names

IMAGENET_CLASSNAMES

Complete list of 1,000 ImageNet class names in the standard ImageNet-1K order. These are human-readable labels corresponding to ImageNet synsets. Examples:
  • "tench", "goldfish", "great white shark"
  • "tabby cat", "tiger cat", "Persian cat"
  • "golden retriever", "labrador retriever"
  • "laptop computer", "desktop computer"
  • "pizza", "cheeseburger", "ice cream"
Total: 1,000 class names covering animals, objects, vehicles, food, and more. Source: src/open_clip/zero_shot_metadata.py:99

Usage Examples

Using SIMPLE_IMAGENET_TEMPLATES

Full ImageNet Classification

Comparing Template Sets

Custom Classes with Pre-defined Templates

Creating Custom Template Variants

Inspecting Template Output

Template Design Notes

Why Multiple Templates?

Using multiple templates improves classification robustness by:
  1. Handling ambiguity - Different phrasings capture different aspects of a concept
  2. Averaging out noise - Multiple templates reduce sensitivity to specific wording
  3. Covering variations - Templates account for different visual presentations (size, quality, style)

Template Selection

  • SIMPLE_IMAGENET_TEMPLATES: Use for faster inference with good accuracy (7 templates)
  • OPENAI_IMAGENET_TEMPLATES: Use for best accuracy when computation time allows (80 templates)
  • Custom templates: Create domain-specific templates for specialized applications

Performance Trade-offs

For large-scale applications, SIMPLE_IMAGENET_TEMPLATES provides the best balance of accuracy and speed.

References